Count and Say
Question
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, …
1 is read off as “one 1” or 11.
11 is read off as “two 1s” or 21.
21 is read off as “one 2, then one 1” or 1211.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
Analysis
两个StringBuilder:pre/cur保存需要被描述的字符串和当前的字符串
- count每次计数应该从1开始,因为不可能出现0个的元素
- 在对一个字符串循环查数结束后,需要
current.append(count).append(tmp)
由于此时最后一个(一种)字符还没有加入字符串中
Code
|
|
Balanced Binary Tree
Question
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Analysis
https://discuss.leetcode.com/topic/7798/the-bottom-up-o-n-solution-would-be-better/2
Code
Original Version
|
|
Top-Down Method
|
|
Down-Top Method
|
|